#include <iostream>
#include <fcntl.h>
#include <unistd.h>
using namespace std;
void NoBlock(int fd)
{
    if (fd < 0)
    {
        perror("NoBlock fail\n");
        exit(1);
    }
    int flags = fd | O_NONBLOCK;
    fcntl(fd, F_SETFL, flags);
}
int main()
{
    NoBlock(0);
    while (1)
    {
        static int i = 1;
        char buffer[1024];
        //fd = 0 从键盘读取
        int n = read(0, buffer, sizeof(buffer) - 1);
        if (n > 0)
        {
            buffer[n-1]='\0';
            cout<<buffer<<endl;
            sleep(1);
        }
        else if (n < 0)
        {
            if (errno == EAGAIN || errno == EWOULDBLOCK)
            {
                cout << "none..." << endl; // 数据未就绪
                sleep(3);
                continue;
            }
            else if (errno == EINTR) // 被信号中断
            {
                continue;
            }
            else
            {
                perror("read fail\n");
            }
        }
        else
        {
            break;
        }
    }
    return 0;
}